<?php

/**
 * model_class
 */

class Model_class extends YOU {

	public function __construct()
	{
		parent::__construct();
	}

	/** 读取总数 **/
	public function count()
	{
		$row = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_class_up` WHERE is_now=1");
		
		return (int)$row['count'];
	}
	public function countStudent($class_id){
        $row = $this->db->fetchRow("SELECT count(1) as count FROM `tbl_student` WHERE class_id = '{$class_id}'");
        return (int)$row['count'];
    }
	/** 根据年级id读取总数 **/
	public function countGread($grade_id)
	{
		$row = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_class_up` WHERE `grade_id`='{$grade_id}' AND is_now='1'");
		
		return (int)$row['count'];
	}


	/** 读取成配置 **/
	public function getIni()
	{
		$class_lists = $this->getAll();
		foreach ($class_lists as $key => $value){
			$class[$value['class_id']] = $value['class_name'];
		}

		return $class;
	}

	/** 根据grade_id读取列表 **/
	public function getListByGrade($grade_id)
	{
		$class_lists = $this->db->fetchAll("SELECT * FROM `tbl_class_up` WHERE is_now =1 AND grade_id='{$grade_id}' ORDER BY class_code ASC");
		
		return $class_lists;
	}

	/** 读取列表 **/
	public function getAll($field = "*")
	{
		$class_lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_class_up` WHERE is_now =1 ORDER BY grade_id ASC");

		return $class_lists;
	}
	/** class_id->最后一条 **/
	public function getClassEnd($class_id, $field = "*")
	{	
		$upc_list = $this->db->fetchRow("SELECT Max(upc_id),class_code FROM `tbl_class_up` WHERE class_id = {$class_id}");
		$upc_list['upc_id'] = $upc_list['Max(upc_id)'];
		return $upc_list;
	}
	/** 取得当前班级历史记录表最大数 **/
	public function getClassBox($grade_id, $field = "*")
	{
		$data = $this->db->fetchRow("SELECT class_box FROM `tbl_class_up` WHERE is_now ='1' AND grade_id='{$grade_id}' ORDER BY class_box DESC");
	
		return $data;
	}
	/** 得到班级数据带年级信息 **/
	public function getGInfo($class_id, $field = "*")
	{
		$class_data = $this->db->fetchRow("SELECT a.*,b.grade_name FROM `tbl_class_up` AS a,`tbl_grade_up` AS b WHERE a.is_now=1 AND b.is_now=1 AND a.grade_id=b.grade_id AND a.class_id='{$class_id}'");

		return $class_data;
	}

	/** 得到班级数据 **/
	public function getInfo($class_id, $field = "*")
	{
		$class_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_class_up` WHERE  is_now =1 AND class_id='{$class_id}'");

		return $class_data;
	}
	

	/** 年级 班级树状数据 **/
	public function getTree(){
		$class_lists = $this->db->fetchAll("SELECT a.class_id,a.class_name,b.grade_id,b.grade_name FROM `tbl_class_up` AS a,`tbl_grade_up` AS b WHERE a.grade_id=b.grade_id AND a.is_now=1 AND b.is_now=1");
		$tree = array();
		foreach ($class_lists as $key => $value){
			$tree[$value['grade_id']]['grade_name']    = $value['grade_name'];
			$tree[$value['grade_id']]['class'][] = array(
			"class_id"   => $value['class_id'],
			"class_name" => $value['class_name'],
			);
		}

		return $tree;
	}
	
	/** 读取名称根据id **/
	public function getNameById($class_id)
	{
		$data = $this->getInfo($class_id, "class_name");

		return $data['class_name'];
	}
	
	/** 得到班级分类所有数据 **/
	public function getClassifyAll( $field = "*")
	{
		$Classify_lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_classify`");

		return $Classify_lists;
	}
	/** 得到班级分类数据 **/
	public function getClassifyinfo( $classify_id, $field = "*")
	{
		$Classify_lists = $this->db->fetchRow("SELECT {$field} FROM `tbl_classify` WHERE classify_id={$classify_id}");

		return $Classify_lists;
	}
	
	/** 得到根据班主任的id得到班级 **/
	public function getClassAllByStaff($staff_id, $field = "*")
	{	
		$apppint_type = 4;
		$lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_appoint` WHERE staff_id='{$staff_id}' AND appoint_type = '{$apppint_type}'");
		foreach($lists as $key => $value){
			$row = $this->db->fetchRow("SELECT * FROM `tbl_class_up` WHERE class_id='{$value['class_id']}' AND is_now=1");
			$class_lists[$row['class_id']] = $row['class_name'];
		}

		return $class_lists;
	}
	/** 得到當前畢業所有年級 **/
	public function getGraduateAlls( $field = "*")
	{
		$class_lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_class_up` WHERE upc_graduate='1' ORDER BY upc_year,class_id DESC ");
		
		return $class_lists;
	}
	/** 得到當前畢業所有年級 **/
	public function getGraduateAll($upc_year, $field = "*")
	{
		$class_lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_class_up` WHERE upc_graduate='1' AND upc_year ='{$upc_year}' ORDER BY upc_year,class_id DESC");
		
		return $class_lists;
	}
	/** 得到当前毕业年級 **/
	public function getGraduateinfo($class_id, $field = "*")
	{
		$class_lists = $this->db->fetchRow("SELECT {$field} FROM `tbl_class_up` WHERE upc_graduate='1' AND class_id ='{$class_id}'");
		
		return $class_lists;
	}
	/** 毕业学生 **/
	public function getStudentInfo($class_id, $field = "*")
	{
		$class_lists = $this->db->fetchRow("SELECT {$field} FROM `tbl_class_up` WHERE  class_id ='{$class_id}'");
		
		return $class_lists;
	}
	/** 所有不重复毕业年份 **/
	public function getYearAll()
	{
		$graduate_lists = $this->db->fetchAll("SELECT distinct graduate_year FROM `tbl_graduate`");
		
		return $graduate_lists;
	}
	/** 毕业学期 **/
	public function getGraduateIn($graduate_id, $field = "*")
	{
		$graduate_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_graduate` WHERE  graduate_id ='{$graduate_id}'");
		
		return $graduate_data;
	}
	/** 毕业学期 **/
	public function getGraduateNew($grade_id, $field = "*")
	{
		$graduate_data = $this->db->fetchAll("SELECT {$field} FROM `tbl_class_up` WHERE grade_id='{$grade_id}' AND upc_graduate ='1'");
		
		return $graduate_data;
	}
	
	
}



?>